<template>
  <div class="account-security">
    <div class="mb10 flex_between">
      <h1 class="font_black3">工作台</h1>
      <div class="welcome-message">
        <span class="username font_black3">{{ userInfo.uname || '' }}，</span>
        <span class="greeting font_black3">{{ getTimeGreeting() }}！</span>
        <span class="welcome-text">欢迎来到工作台</span>
      </div>
    </div>
    <div class="user-card">
      <div class="flex_between flex1" style="gap: 16px;">
        <div class="flex_row">
          <div class="user-avatar">
            <div class="avatar-img" v-if="cmpanyInfo.logo">
              <img :src="cmpanyInfo.logo || defaultImg" alt="用户头像" class="avatar-image">
            </div>
            <div class="avatar" v-else>
              <i class="el-icon-user"></i>
            </div>
          </div>
          <div class="user-details ml20">
            <h2>{{ cmpanyInfo.name }}</h2>
            <p class="font_size16 font_black"> {{ userInfo.uname }} </p>
            <div class="user-status-info">
              <p class="font_size14">到期时间：{{ cmpanyInfo.endtime || '--' }}</p>
              <p class="font_size14">版本信息：
                <el-tag type="success" size="mini" class="mr10">{{ cmpanyInfo.v_title || '--' }}</el-tag>
                <el-button icon="el-icon-s-promotion" type="text" @click="changeHandle" size="mini"
                  class="under-line">换版本</el-button>
              </p>
            </div>
          </div>
        </div>
        <div class="basic-info-container flex_row">
          <div class="info-card">
            <div class="info-icon">
              <i class="el-icon-mobile-phone" style="font-size: 26px;"></i>
            </div>
            <div class="info-content">
              <span class="info-label">登录账号</span>
              <span class="info-value">{{ userInfo.cellphone }}</span>
            </div>
          </div>
          <div class="info-card">
            <div class="info-icon">
              <i class="el-icon-user" style="font-size: 26px;"></i>
            </div>
            <div class="info-content">
              <span class="info-label">真实姓名</span>
              <span class="info-value">{{ userInfo.uname }}</span>
            </div>
          </div>
          <div class="info-card">
            <div class="info-icon">
              <i class="el-icon-position" style="font-size: 26px;"></i>
            </div>
            <div class="info-content">
              <span class="info-label">职位角色</span>
              <span class="info-value">{{ userInfo.position_title || '未定职位' }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="security-card">
      <div class="dashboard-stats">
        <div class="stat-card flex_between">
          <div class="stat-icon">
            <i class="el-icon-user"></i>
          </div>
          <div class="stat-content flex_row" @click="goTTo('customer')">
            <div class="stat-label">今日新客量</div>
            <div class="stat-value ml10">{{ (menu2Info.menu_info2 && menu2Info.menu_info2.cy_ls &&
              menu2Info.menu_info2.cy_ls[0].title2) || 0 }}</div>
          </div>
        </div>
        <div class="stat-card flex_between" @click="goTTo('saleReport')">
          <div class="stat-icon">
            <i class="el-icon-s-data"></i>
          </div>
          <div class="stat-content flex_row">
            <div class="stat-label">今日销售额</div>
            <div class="stat-value ml10">{{ (menu2Info.menu_info2 && menu2Info.menu_info2.cy_ls &&
              menu2Info.menu_info2.cy_ls[1].title2) || 0 }}</div>
          </div>
        </div>
        <div class="stat-card flex_between" @click="goTTo('appointment')">
          <div class="stat-icon">
            <i class="el-icon-suitcase"></i>
          </div>
          <div class="stat-content flex_row">
            <div class="stat-label">今日项目额</div>
            <div class="stat-value ml10">{{ (menu2Info.menu_info2 && menu2Info.menu_info2.cy_ls &&
              menu2Info.menu_info2.cy_ls[2].title2) || 0 }}</div>
          </div>
        </div>
      </div>
      <div class="nav-tabs">
        <div v-for="tab in ksdh_ls" :key="tab.id" class="nav-tab" :class="{ active: activeTab == tab.id }"
          @click="switchTab(tab)">
          {{ tab.title }}
        </div>
      </div>
      <div class="tab-content" style="padding-top: 5px;">
        <div class="feature-grid">
          <div class="feature-card" v-for="item in ksdh_ls_menu" :key="item.id" @click="goToPage(item)">
            <div class="feature-icon">
              <i :class="getMenuItemIcon(item)"></i>
            </div>
            <div class="feature-label">{{ item.title }}</div>
          </div>
        </div>
      </div>
    </div>
    <changeVersionDialog ref="changeDialogRef"></changeVersionDialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, getCurrentInstance } from '@vue/composition-api'
import { getters } from 'vuex'
import config from "@/js/conf/Config.js"
import localStore from "@/js/LocalStore.js"
const { proxy } = getCurrentInstance()
const cpInfo = ref(localStore.getCpInfo() || {});
const userInfo = ref(localStore.getUserInfo() || {});
import { AccessCtrlRoute } from "@/js/limit/AccessCtrlRoute.js";

import DEFAULT_IMG from "@/static/images/Uimg.png";
const defaultImg = ref(DEFAULT_IMG);
import changeVersionDialog from '@/pages/setting/components/changeVersionDialog.vue'
uni.setLeftWindowStyle({ width: "72px" }); //设置左边导航宽度

// 概览-获取
const menu2Info = ref({})
const ksdh_ls = ref([])
const ksdh_ls_menu = ref([])
const getMenu2Info = async () => {
  try {
    let res = await proxy.$api({
      url: config.compset_staff_menu2_info, data: {}
    })
    let result = res.data || {}
    if (res.status == 1) {
      menu2Info.value = result;
      ksdh_ls.value = result?.menu_info2?.ksdh_ls || []
      ksdh_ls.value.forEach((item, index) => {
        if (index == 0) {
          item.ls = item.ls?.slice(0, 5) || []
        } else {
          item.ls = item.ls || []
        }
      })
      ksdh_ls_menu.value = result?.menu_info2?.ksdh_ls[0]?.ls?.slice(0, 5) || []
    }
  } catch (error) { }
}

// 获取详情
const cmpanyInfo = ref({})
const getCmpanyInfo = async () => {
	try {
		let res = await proxy.$api({ url: config.compset_company_info, data: {} })
		let result = res.data || {}
		if (res.status == 1 && result) {
			cmpanyInfo.value = {
				...result,
			}
			proxy.$store.commit('common/set_cmpany_info', result);
		}
	} catch (error) { }
}
getCmpanyInfo()

// 组件挂载时初始化数据
onMounted(() => {
  getMenu2Info()
  uni.setLeftWindowStyle({ width: "72px" }); //设置左边导航宽度
})

// 切换版本
const changeDialogRef = ref(null)
const changeHandle = () => {
  changeDialogRef.value.open()
}

// 当前激活的标签
const activeTab = ref(1);

// 切换标签
const switchTab = (tab) => {
  activeTab.value = tab.id;
  ksdh_ls_menu.value = tab.ls || []
};

// 在script部分的适当位置添加getMenuItemIcon函数
// 页面跳转函数
const goToPage = (pare) => {
  let isAccess = false
  AccessCtrlRoute.forEach(item => {
    if (pare.rck?.split(',')?.map(Number)?.includes(item?.accessId) && activeTab.value == item?.fid) {
      if (item.access) {
        isAccess = true
        if (item?.isSetting) {
          uni.redirectTo({
            url: '/pages/setting/index?id=' + item.path
          })
        } else {
          uni.redirectTo({
            url: item.path
          })
        }
      } else {
        isAccess = false
      }
      return false; // 跳出循环
    }
  })
  if (!isAccess) {
    proxy.$message({
      message: '您没有权限访问该页面',
      type: 'warning'
    })
  }
};

// 今日卡片跳转
const goTTo = (type) => {
  let path = ''
  if(type == 'saleReport'){
    path = '/pages/report/saleReport?from=accSecury'
  }else if(type == 'appointment'){
    path = '/pages/report/appointment?from=accSecury'
  }else if(type == 'customer'){
    path = 'pages/customer/index_v2?from=accSecury'
  }
  uni.redirectTo({
    url: path
  })
}

const getMenuItemIcon = (item) => {
  const routeItem = AccessCtrlRoute.find(route => route.title == item.title && activeTab.value == route.fid
  );
  return routeItem?.icon || 'el-icon-folder';
};

// 根据当前时间获取问候语
const getTimeGreeting = () => {
  const hour = new Date().getHours();
  if (hour >= 5 && hour < 12) {
    return '早上好';
  } else if (hour >= 12 && hour < 18) {
    return '下午好';
  } else {
    return '晚上好';
  }
};
</script>

<style scoped lang="scss">
/* 全局变量定义 */
$primary-color: #1890ff;
$primary-light: #e6f7ff;
$primary-dark: #096dd9;
$success-color: #52c41a;
$warning-color: #faad14;
$error-color: #f5222d;
$text-primary: #262626;
$text-secondary: #595959;
$text-tertiary: #8c8c8c;
$bg-color: #f5f7fa;
$card-bg: #ffffff;
$border-color: #e8e8e8;
$shadow-light: 0 2px 8px rgba(0, 0, 0, 0.06);
$shadow-medium: 0 4px 16px rgba(0, 0, 0, 0.08);
$border-radius: 8px;
$transition: all 0.3s ease;

// 工具类
.flex-row {
  display: flex;
  flex-direction: row;
}

.flex-column {
  display: flex;
  flex-direction: column;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.flex1 {
  flex: 1;
}

.ml10 {
  margin-left: 10px;
}

.ml20 {
  margin-left: 20px;
}

.mr10 {
  margin-right: 10px;
}

.font_size12 {
  font-size: 12px;
}

.font_size14 {
  font-size: 14px;
}

.font_size16 {
  font-size: 16px;
}

.under-line {
  text-decoration: underline;
}

/* 主容器样式 */
.account-security {
  margin: 0 auto;
  padding: 24px;
  padding-top: 10px;
}

.welcome-message {
  font-size: 16px;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: 25px;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, $primary-color, transparent);
    transform: translateX(-100%);
    transition: transform 0.5s ease;
  }
  &:hover {
    border-color: rgba(24, 144, 255, 0.4);
    &::before {
      transform: translateX(100%);
    }
  }
}

.username {
  font-weight: 600;
  font-size: 18px;
}

.greeting {
  color: $text-primary;
  font-weight: 600;
}

.welcome-text {
  color: $text-secondary;
  font-size: 16px;
  font-weight: 500;
}

/* 用户信息卡片样式 */
.user-card {
  background: $card-bg;
  border-radius: $border-radius;
  padding: 24px;
  margin-bottom: 12px;
  box-shadow: $shadow-light;
  border: 1px solid $border-color;
  display: flex;
  align-items: center;
  gap: 24px;
  transition: $transition;
}

/* 用户头像样式 */
.user-avatar {
  text-align: center;

  .avatar-img {
    height: 100px;
    width: 100px;
    border-radius: 4px;
    object-fit: cover;
    border: 3px solid $primary-light;
    box-shadow: $shadow-light;
    margin: 0 auto;
    transition: $transition;

    &:hover {
      transform: scale(1.05);
      box-shadow: $shadow-medium;
    }
  }

  .avatar-image {
    height: 100%;
    width: 100%;
    border-radius: 4px;
    object-fit: cover;
  }

  .avatar {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: $shadow-light;
    transition: $transition;

    &:hover {
      transform: scale(1.05);
      box-shadow: $shadow-medium;
    }

    i {
      font-size: 40px;
      color: white;
    }
  }
}

/* 用户详情样式 */
.user-details {
  flex: 1;
  min-width: 200px;

  h2 {
    font-size: 24px;
    color: $text-primary;
    font-weight: 600;
  }

  .font_size16 {
    font-size: 16px;
    color: $text-secondary;
  }

  .user-status-info {
    display: flex;
    flex-direction: column;
  }

  .font_size14 {
    font-size: 14px;
    margin: 0;
    color: $text-tertiary;
    display: flex;
    align-items: center;
  }

  .el-button {
    transition: $transition;

    &:hover {
      color: $primary-dark;
    }
  }

  .el-tag {
    font-size: 12px;
  }
}

/* 基本信息容器样式 */
.basic-info-container {
  display: flex;
  gap: 16px;
  flex: 1;
  justify-content: flex-end;
  padding: 0;
}

/* 信息卡片样式 */
.info-card {
  display: flex;
  align-items: center;
  // background: $primary-light;
  // border: 1px solid $border-color;
  // border-radius: $border-radius;
  padding: 16px 20px;
  min-width: 200px;
  // box-shadow: $shadow-light;
  // transition: $transition;
}

/* 信息图标样式 */
.info-icon {
  width: 40px;
  height: 40px;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  transition: $transition;

  i {
    color: $primary-color;
    font-size: 18px;
    transition: $transition;
  }
}

/* 信息内容样式 */
.info-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* 信息标签样式 */
.info-label {
  font-size: 12px;
  color: #888;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: $transition;
}

/* 信息值样式 */
.info-value {
  font-size: 16px;
  color: $text-primary;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: $transition;
}

/* 安全卡片样式 */
.security-card {
  background: $card-bg;
  border-radius: $border-radius;
  padding: 24px;
  // margin-bottom: 24px;
  box-shadow: $shadow-light;
  border: 1px solid $border-color;
}

/* 数据概览卡片样式 */
.dashboard-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  margin-bottom: 6px;
}

.stat-card {
  flex: 1;
  background: $bg-color;
  border-radius: $border-radius;
  padding: 20px;
  display: flex;
  align-items: center;
  border: 1px solid $border-color;
  transition: $transition;

  &:hover {
    background: $primary-light;
    border-color: $primary-color;
    transform: translateY(-2px);
    box-shadow: $shadow-medium;
  }
}

.stat-icon {
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, #5874f1 0%, #80c4f1 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  margin-right: 20px;
  box-shadow: $shadow-light;
}

.stat-content {
  display: flex;
  flex-direction: row;
  gap: 8px;
  flex: 1;
  margin-left: 120px;
}

.stat-label {
  font-size: 16px;
  color: $text-tertiary;
  font-weight: 500;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: $text-primary;
}

/* 导航标签样式 */
.nav-tabs {
  display: flex;
  border-bottom: 1px solid $border-color;
  margin-bottom: 12px;
  padding-bottom: 0;
}

.nav-tab {
  padding: 12px 24px;
  cursor: pointer;
  font-size: 16px;
  color: $text-secondary;
  border-bottom: 1px solid transparent;
  transition: $transition;
  position: relative;
  font-weight: 500;

  &.active {
    color: $primary-color;
    font-weight: 600;
    border-bottom-color: $primary-color;
  }

  &.active::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 2px;
    background-color: $primary-color;
  }
}

.tab-content {
  height: 420px;
  overflow-y: auto;
}


.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

.feature-card {
  background: $card-bg;
  border: 1px solid $border-color;
  border-radius: $border-radius;
  padding: 24px;
  text-align: center;
  cursor: pointer;
  transition: $transition;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, $primary-color, $success-color);
    transition: $transition;
  }

  &:hover {
    border-color: $primary-color;
    box-shadow: $shadow-medium;
    transform: translateY(-2px);

    &::before {
      left: 0;
    }

    .feature-icon {
      background: linear-gradient(135deg, $primary-color, $primary-dark);

      i {
        color: white;
      }
    }
  }
}

.feature-icon {
  width: 80px;
  height: 80px;
  background: $bg-color;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 16px;
  font-size: 32px;
  color: $primary-color;
  transition: $transition;
  box-shadow: $shadow-light;
}

.feature-label {
  font-size: 16px;
  color: $text-primary;
  font-weight: 500;
}

/* 空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  color: $text-tertiary;

  i {
    font-size: 80px;
    margin-bottom: 20px;
    opacity: 0.3;
  }

  p {
    font-size: 18px;
  }
}
</style>
